﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace printer
{
    public static class DataBase
    {
        //contain all data base functions, contain constructor that start the connection
        //constructor  start connection(database_name)
        //function insert(table name,query)
        //function update(table,query)
        //function delete(table,query)
        // function close connection
        public static SqlConnection scon;
        public static SqlCommand scom;
       
        /// <summary>
        /// constructor that start the connection with database
        /// server name local or on server , database name , security on the pc or on the server
        /// </summary>
        /// <param name="data_base_name"></param>
        //public static bool connect(string data_base_name)
        //{
        //    string connectionString = "server=localhost\\SQLEXPRESS ;database=" + data_base_name + "; integrated security=true;MultipleActiveResultSets=true";
        //    using (SqlConnection scon = new SqlConnection(connectionString))
        //    {
        //        try
        //        {
        //            scon.Open();

        //            return (scon.State == ConnectionState.Open);
        //        }
        //        catch
        //        {
        //            return false;
        //        }
        //    }

        //}


        public static void connect(string data_base_name)
        {

            scon = new SqlConnection(); //
            scon.ConnectionString = "server=localhost\\SQLEXPRESS ;database=" + data_base_name + "; integrated security=true;MultipleActiveResultSets=true";
            scon.Open();
        }


        /// <summary>
        /// select data from data base
        /// </summary>
        /// <param name="select_command"></param>
        /// <returns> we take the data reader in sqldatareader
        /// while (reader.Read())
          ///  {
          ///      MessageBox.Show(reader["part_num"].ToString());
         ///   }
        /// </returns>
        public static SqlDataReader select_from_database(string select_command) 
        {
            try
            {
                //2- prepare what will you say
                scom = new SqlCommand();
                scom.Connection = scon;
                //  cm.CommandText = "select count(*) from employee";
                scom.CommandText = select_command;
                SqlDataReader reader = scom.ExecuteReader();
                
                return reader;
          
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// insert or update or delete data from  data base
        /// </summary>
        /// <param name="insert_command"> </param>
        public static int insert_update_delete_in_database(string command)
        {
            //2- prepare what will you say
            scom = new SqlCommand();
            scom.Connection = scon;
            scom.CommandText = command;
           return scom.ExecuteNonQuery();
        }


        /// <summary>
        /// close the connection with data base
        /// </summary>
        public static void close_connection()
        {
            scon.Dispose();
            scon = null;
        }
        
    }
}
